﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Portal.Entity;
using Portal.Interface;
using MyLib;
using System.Data;
using System.Text;
using System.IO;

namespace WebPortal.YWYY
{
    public partial class PersonTB : System.Web.UI.Page
    {
        IOrg _org = ServiceContainer.Instance().GetService<IOrg>();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                UserEntity user = _org.GetUserEntity(Page.User.Identity.Name);
                tarcode.Text = user.LoginName.Replace("\\", "/");
                tarq.Text = DateTime.Now.ToString("yyyy-MM-dd");
                if (tarcode.Text == "")
                {
                    string aa = "<script>alert('没有获取到该用户的信息，请重新登录框架！');window.close();</script>";

                    if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "welcome"))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "welcome", aa);
                    }
                }
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"select ename,zhr_otext from zhr_employee_oa where pernr='{0}'", tarcode.Text.Replace("ULTIMUS/", ""));

                DataTable dt = DataAccess.Instance("BusinessDB").ExecuteDataTable(sb.ToString());
                if (dt != null && dt.Rows.Count > 0)
                {
                    tar.Text = dt.Rows[0][0].ToString();
                    szbm.Text = dt.Rows[0][1].ToString();
                }
                else
                {
                    string bb = "<script>alert('没有获取到该用户的信息，请联系管理员！');window.close();</script>";

                    if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "welcome"))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "welcome", bb);
                    }
                }
            }
            catch (Exception ww)
            {
                Response.Write("<script type='text/javascript'>alert('" + ww.ToString() + "');</script>");
            }
        }
        //提交表单
        protected void tijiao_Click(object sender, EventArgs e)
        {
            //isnull();
            submit("1");
        }
        //确认提案完结
        protected void wanjie_Click(object sender, EventArgs e)
        {
            //isnull();
            submit("2");
        }
        //查看历史记录
        protected void history_Click(object sender, EventArgs e)
        {
            Response.Redirect("HistoryTB.aspx?tianname=" + tamc.Text.Trim(), false);
        }
        //提交表单数据,1提交，2完结
        public void submit(string type)
        {
            try
            {
                string TIANRENNAME = tar.Text;
                string TIANRENCODE = tarcode.Text;
                string TIANDATE = tarq.Text;
                string TIANBUMEN = szbm.Text;
                string TIANNAME = tamc.Text.Trim();
                string TIANSUMMARY = tajy.Text;
                string DIAOCHAFF = dcff.Text;
                string DIAOCHAQK = dcqk.Text;
                string ZILIAOZL = zlzl.Text;
                string ATTACHMENT = aa.Value;
                string scriptstrs = "";

                if (string.IsNullOrEmpty(TIANNAME))
                {
                    scriptstrs = "<script>alert('请填写或选择提案名称！');</script>";
                    if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "aa"))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "aa", scriptstrs);
                    }
                    return;
                }
                

                StringBuilder sb = new StringBuilder();

                sb.AppendFormat(@"select * from PROC_FWDY_TIBAOSHENQING where tianrencode='{0}' and tianname='{1}'", TIANRENCODE.Trim(), TIANNAME.Trim());
                DataTable t = DataAccess.Instance("BizDB").ExecuteDataTable(sb.ToString());
                if (t == null || t.Rows.Count == 0)
                {
                    sb = new StringBuilder();
                    sb.AppendFormat(@"insert into PROC_FWDY_TIBAOSHENQING(tianrenname, tianrencode, tiandate, tianbumen, tianname, tiansummary, diaochaff, diaochaqk, ziliaozl,ATTACHMENT) values ('{0}','{1}',to_date('{2}','yyyy-MM-dd'),'{3}','{4}','{5}','{6}','{7}','{8}','{9}')", TIANRENNAME, TIANRENCODE, TIANDATE, TIANBUMEN, TIANNAME, TIANSUMMARY, DIAOCHAFF, DIAOCHAQK, ZILIAOZL,ATTACHMENT);

                    int success = DataAccess.Instance("BizDB").ExecuteNonQuery(sb.ToString());
                    if (success > 0)
                    {
                        if (type == "1")
                        {
                            scriptstrs = "<script>alert('提交成功！');</script>";
                        }
                        if (type == "2")
                        {
                            scriptstrs = "<script>alert('成功完结！');</script>";
                        }
                        if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "bb"))
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "bb", scriptstrs);
                        }
                    }
                }
                else
                {
                    sb = new StringBuilder();
                    if (type == "1")
                    {
                        sb.AppendFormat(@"update PROC_FWDY_TIBAOSHENQING set tiansummary='{0}', diaochaff='{1}', diaochaqk='{2}', ziliaozl='{3}',ATTACHMENT='{6}' where tianrencode='{4}' and tianname='{5}'", TIANSUMMARY, DIAOCHAFF, DIAOCHAQK, ZILIAOZL, TIANRENCODE.Trim(), TIANNAME.Trim(),ATTACHMENT);

                        int success = DataAccess.Instance("BizDB").ExecuteNonQuery(sb.ToString());
                        if (success > 0)
                        {
                            scriptstrs = "<script>alert('提交成功！');</script>";

                            if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "cc"))
                            {
                                Page.ClientScript.RegisterStartupScript(this.GetType(), "cc", scriptstrs);
                            }
                        }
                    }
                    if (type == "2")
                    {
                        sb.AppendFormat(@"update PROC_FWDY_TIBAOSHENQING set tiansummary='{0}', diaochaff='{1}', diaochaqk='{2}', ziliaozl='{3}',status='1',ATTACHMENT='{6}' where tianrencode='{4}' and tianname='{5}'", TIANSUMMARY, DIAOCHAFF, DIAOCHAQK, ZILIAOZL, TIANRENCODE.Trim(), TIANNAME.Trim(),ATTACHMENT);
                        int yes = DataAccess.Instance("BizDB").ExecuteNonQuery(sb.ToString());
                        if (yes > 0)
                        {
                            scriptstrs = "<script>alert('成功完结！');</script>";
                            if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "dd"))
                            {
                                Page.ClientScript.RegisterStartupScript(this.GetType(), "dd", scriptstrs);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                string str = "<script>alert('" + e.ToString() + "');</script>";
                if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "ee"))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "ee", str);
                }
            }
        }
        //上传附件
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                // Get the HttpFileCollection

                HttpFileCollection hfc = Request.Files;

                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];

                    if (hpf.ContentLength > 0)
                    {
                        aa.Value = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + System.IO.Path.GetFileName(hpf.FileName);
                        hpf.SaveAs(Server.MapPath("../PersonTB") + "\\" + aa.Value);
                        attachment.Text = System.IO.Path.GetFileName(hpf.FileName);

                        //Response.Write("<b>File: </b>" + hpf.FileName + " <b>Size:</b> " +hpf.ContentLength + " <b>Type:</b> " + hpf.ContentType + " Uploaded Successfully <br/>");
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        //下载附件
        protected void attachment_Click(object sender, EventArgs e)
        {
            string fileName = attachment.Text;//客户端保存的文件名 
            string filePath = Server.MapPath("../PersonTB/") + aa.Value;//路径 
            //以字符流的形式下载文件 
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开 
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

        //public void isnull()
        //{
        //    string TIANRENNAME = tar.Text;
        //    string TIANRENCODE = tarcode.Text;
        //    string TIANDATE = tarq.Text;
        //    string TIANBUMEN = szbm.Text;
        //    string TIANNAME = tamc.Text;
        //    string TIANSUMMARY = tajy.Text;
        //    string DIAOCHAFF = dcff.Text;
        //    string DIAOCHAQK = dcqk.Text;
        //    string ZILIAOZL = zlzl.Text;
        //    if (string.IsNullOrEmpty(TIANRENNAME) && string.IsNullOrEmpty(TIANDATE) && string.IsNullOrEmpty(TIANBUMEN) && string.IsNullOrEmpty(TIANNAME) && string.IsNullOrEmpty(TIANSUMMARY) && string.IsNullOrEmpty(DIAOCHAFF) && string.IsNullOrEmpty(DIAOCHAQK) && string.IsNullOrEmpty(ZILIAOZL))
        //    {
        //        string scriptstrs = "<script>alert('表单上红色星号的区域必须填写！');</script>";
        //        if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "welcome"))
        //        {
        //            Page.ClientScript.RegisterStartupScript(this.GetType(), "welcome", scriptstrs);
        //        }
        //    }
        //    return;
        //}
    }
}